import axios from "axios";
import router from '../router'
import {Ini} from "./ini";
import Vue from 'vue'
let enviroment = Ini.readIni("data", "env");
let baseURL = "https://bos.kquestions.com/portal";
if (enviroment === "development_client") {
    baseURL = "https://dev.bos.kquestions.com/portal"
    // baseURL = "http://192.168.1.83:8080/portal";
}

// create an axios instance
const service = axios.create({
    // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    baseURL: baseURL,
    withCredentials: true, // send cookies when cross-domain requests
    timeout: 15000, // request timeout
});
// request interceptor
service.interceptors.request.use(
    (config) => {
        // do something before request is sent
        if (localStorage.accesstoken) {
            config.headers.token = localStorage.accesstoken;
            // config.headers.ContentType = 'application/json;charset=UTF-8';
        }
        if (config.method === "post") {
            let eduLevel =  Vue.prototype.$readIniFile('data','eduLevel')
            // console.log(eduLevel);
            config.data.appkey = eduLevel === 'primary' ? 'NOVO_EDU_BEAUTY_SCIENCE_PC' : 'NOVO_EDU_QIAN_WEN_PC'
            let versionFlag =  Vue.prototype.$readIniFile('data','versionInfo')
            config.data.versionFlag = versionFlag 
            if(localStorage.accesstoken){
                config.data.accessToken = localStorage.accesstoken;
            }
        }
        // if (config.method === "get") {
        //     config.params.appKey = "NOVO_EDU_BEAUTY_SCIENCE_PC";
        // }
        return config;
    },
    (error) => {
        // do something with request error
        console.log(error); // for debug
        return Promise.reject(error);
    }
);

// response interceptor
service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    (response) => {
        const res = response.data;

        // if the custom code is not 20000, it is judged as an error.
        if (res.code == -9999) {
            if (response.config.url.indexOf('/user/switchToken') > -1) {
                localStorage.removeItem("isOffline");
                localStorage.removeItem("accesstoken");
                localStorage.removeItem("refreshToken");
                router.push({name: 'login'})
                return
            }

            if (response.config.url.indexOf('/user/updateUseTime') > -1) {
                return Promise.reject(res);
            }
        }
        if (res.code == -1000) {
            if(response.config.url.indexOf('/tick') > -1){
                return res;
            }
            router.push({name: 'login'})
            localStorage.removeItem("accesstoken");
            return Promise.reject(res);
        }
        if (res.code == -32) {
            localStorage.removeItem("isOffline");
            localStorage.removeItem("accesstoken");
            localStorage.removeItem("refreshToken");
            router.push({name: 'login'})
            return
        }

        return res;
    },
    (error) => {

        return Promise.reject(error);
    }
);

export default service;
